//
//  NSObject+NEDKVO.m
//  NEDSuoShi
//
//  Created by 000 on 2020/12/4.
//  Copyright © 2020 林宁宁. All rights reserved.
//

#import "NSObject+NEDKVO.h"
#import <YYCategories/NSObject+YYAddForKVO.h>
#import <KVOController/NSObject+FBKVOController.h>

@implementation NSObject (NEDKVO)

- (void)ned_kvoForKeyPaths:(NSArray <NSString*> *)keyPaths
                  observe:(id)observe
                    block:(nonnull void (^)(id _Nonnull, id _Nullable, id _Nullable))block{
    for(NSString * keyPath in keyPaths){
        [self ned_kvoForKeyPath:keyPath observe:observe block:block];
    }
}

- (void)ned_kvoForKeyPath:(NSString*)keyPath
                  observe:(id)observe
                   block:(nonnull void (^)(id _Nonnull, id _Nullable, id _Nullable))block{
    if([self isEqual:observe]){
        [self addObserverBlockForKeyPath:keyPath block:block];
        return;
    }
    [self.KVOController observe:observe keyPath:keyPath options:(NSKeyValueObservingOptionNew) block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        id oldVal = [change objectForKey:NSKeyValueChangeOldKey];
        if (oldVal == [NSNull null]) oldVal = nil;
        
        id newVal = [change objectForKey:NSKeyValueChangeNewKey];
        if (newVal == [NSNull null]) newVal = nil;
        
        if(block){
            block(object,oldVal,newVal);
        }
    }];
}


@end
